local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"
local tabletool = require "tabletool"
require "pszenum"

local TablesvrHelper = {}
local server

function TablesvrHelper:init(base)
    server = base
end

function TablesvrHelper:get_server()
    return server
end

function TablesvrHelper:sendmsg_to_alltableplayer(msgname, msg, ...)
    local table_data = server.table_data
    local rids = {}
    for _, seat in ipairs(table_data.seats or {}) do
        if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and not seat.isrobot then
        -- if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
            if not seat.waitreenter then
                table.insert(rids, seat.rid)
            end
        end
    end

    for _, v in pairs(table_data.standing) do
        if not v[2] then
            table.insert(rids, v[1])
        end
    end
    cluster.send("gatenode", ".router", "notify", rids, "psz." .. msgname, msg, ...)
end

function TablesvrHelper:sendmsg_to_tableplayer(seat, msgname, ...)
    if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and not seat.isrobot then
    -- if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
        if not seat.waitreenter then
            cluster.send("gatenode", ".router", "notify", {seat.rid}, "psz." .. msgname, ...)
        end
    end
end

function TablesvrHelper:sendmsg_to_roomsvr(msgname, ...)
    skynet.send(".router", "lua", msgname, ...)
end

function TablesvrHelper:callmsg_to_roomsvr(msgname, ...)
    return skynet.call(".router", "lua", msgname, ...)
end

function TablesvrHelper:sendmsg_to_datasvr(msgname, ...)
    cluster.send("datanode", ".router", msgname, ...)
end

function TablesvrHelper:sendmsg_to_gatesvr(msgname, ...)
    cluster.send("gatenode", ".router", msgname, ...)
end

function TablesvrHelper:callmsg_to_gatesvr(msgname, ...)
    local stat, ret = pcall(cluster.call, "gatenode", ".router", msgname, ...)
    if not stat then return false end
    return ret
end

function TablesvrHelper:copy_table_gameinfo(gameinfo)
    TablesvrHelper:copy_gameinfo(gameinfo)
    if gameinfo.room_type == ERoomType.ROOM_TYPE_DIAMOND
	or gameinfo.room_type == ERoomType.ROOM_TYPE_COIN
	or gameinfo.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
        --只要桌子在等待最小玩家数，将接收到的all_bets清零
        if gameinfo.state == ETableState.TABLE_STATE_WAIT_MIN_PLAYER then
            gameinfo.all_bets = 0
            gameinfo.game_turns = 0
        end
    end
end

function TablesvrHelper:copy_gameinfo(gameinfo)
    local table_data = server.table_data
    gameinfo.idx = table_data.idx
    gameinfo.state = table_data.state
    gameinfo.name = table_data.conf.name
    gameinfo.room_type = table_data.conf.room_type
    gameinfo.game_type = table_data.conf.game_type
    gameinfo.max_player_num = table_data.conf.max_player_num
    gameinfo.cur_player_num = table_data.sitdown_player_num
    gameinfo.create_user_rid = table_data.conf.create_user_rid
    gameinfo.create_user_rolename = table_data.conf.create_user_rolename
    gameinfo.create_time = table_data.conf.create_time
    gameinfo.table_code = table_data.conf.table_code           
    gameinfo.create_user_logo = table_data.conf.create_user_logo
    gameinfo.min_carry_diamond = table_data.conf.min_carry_diamond 
    gameinfo.leave_carry_diamond = table_data.conf.leave_carry_diamond 
    gameinfo.base_score = table_data.conf.base_score
    gameinfo.action_timeout = table_data.conf.action_timeout
    gameinfo.action_timeout_count = table_data.conf.action_timeout_count

    gameinfo.action_seat_index = table_data.seat_index
    gameinfo.action_to_time = table_data.action_to_time
    gameinfo.the_round = table_data.the_round
    gameinfo.game_turns = table_data.turns
    gameinfo.force_overturns = table_data.conf.force_overturns
    gameinfo.table_makers = table_data.makers
    gameinfo.all_bets = table_data.all_bets
    gameinfo.cur_bets = table_data.cur_bets
    --TO ADD添加牌信息

    gameinfo.seats = {}
    gameinfo.player_infos = {}
    local seatinfo, playerinfo
    for index, seat in pairs(table_data.seats) do
        seatinfo = {}
        playerinfo = {}
        self:copy_seatinfo(seatinfo, seat)
        table.insert(gameinfo.seats, seatinfo)
        self:copy_playerinfo(playerinfo, seat)
        table.insert(gameinfo.player_infos, playerinfo)
    end
    gameinfo.is_public = table_data.conf.is_public
    gameinfo.tip = table_data.tip or 0
    gameinfo.jackpottip = table_data.jacktip or 0
    gameinfo.curr_round_code = table_data.conf.curr_round_code
    gameinfo.jackopt_num = table_data.jackopt_info.jackopt_num
    gameinfo.jackopt_money_type = table_data.jackopt_info.money_type
    gameinfo.curr_game_times = table_data.cur_games_times
    gameinfo.total_game_times = table_data.conf.game_times
end

function TablesvrHelper:copy_seatinfo(seatinfo, seat)
    seatinfo.rid = seat.rid
    seatinfo.index = seat.index
    seatinfo.state = seat.state
    seatinfo.is_tuoguan = seat.is_tuoguan
    seatinfo.diamond = seat.diamond
    seatinfo.cardsnum = #seat.cards
    seatinfo.seecards = seat.seecards or 0      --是否看牌
    seatinfo.is_ready = seat.is_ready or 0
    seatinfo.ready_to_time = seat.ready_to_time or 0    -- 准备倒计时
    seatinfo.seat_all_bets = seat.diamond_base - seat.diamond
end

function TablesvrHelper:copy_playerinfo(playerinfo, seat)
    playerinfo.rid = seat.rid
    playerinfo.rolename = seat.playerinfo.rolename
    playerinfo.logo = seat.playerinfo.logo
    playerinfo.sex = seat.playerinfo.sex
    playerinfo.ip_addr = seat.playerinfo.ip_addr
    playerinfo.fangka = seat.playerinfo.fangka or 0
    playerinfo.diamond = seat.playerinfo.diamond or 0
    playerinfo.coin = seat.playerinfo.coin or 0
    playerinfo.introduce = seat.playerinfo.introduce or ''
    playerinfo.upvoted_num = seat.playerinfo.upvoted_num
    playerinfo.downvoted_num = seat.playerinfo.downvoted_num
    playerinfo.gps_addr = seat.playerinfo.gps_addr
    playerinfo.logoframe = seat.playerinfo.logoframe or 100
end

function TablesvrHelper:copy_playergameendinfo(playerinfos)
    local table_data = server.table_data
    for index, seat in pairs(table_data.seats) do
        playerinfos[#playerinfos + 1] = {
            rid = seat.rid,
            alldiamond = seat.diamond,
            getdiamond = seat.getdiamond,
        }
    end
end

return  TablesvrHelper